# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Maintainer: Michael Pirogov <vbnet.ru@gmail.com>
pkgname=loki
pkgver=2.9.4
pkgrel=4
pkgdesc="Like Prometheus, but for logs"
pkgusers="loki"
pkggroups="grafana"
options="net"
url="https://github.com/grafana/loki"
license="Apache-2.0"
# limited by 64 bit, ppc64 and riscv64 fails tests
arch="x86_64 aarch64"
makedepends="go bash"
checkdepends="tzdata"
subpackages="$pkgname-logcli $pkgname-promtail-openrc:promtail_openrc $pkgname-promtail $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/grafana/loki/archive/refs/tags/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-promtail.confd
	$pkgname-promtail.initd
	goflags.patch
	enable-cgo.patch
	"
install="$pkgname.pre-install"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOFLAGS="$GOFLAGS -modcacherw -trimpath"

build() {
	make -j1 \
		BUILD_IN_CONTAINER=false \
		GOMOD=readonly \
		GIT_REVISION="alpine-r$pkgrel" \
		GIT_BRANCH=main \
		IMAGE_TAG=$pkgver \
		logcli loki promtail
}

check() {
	# not stable test for fluent-bit
	# tsdb/index fails on aarch64
	# shell check suggests to break it
	# shellcheck disable=SC2046
	TZ=UTC GOGC=10 go test -p=2 $(go list ./... | grep -Ev 'fluent-bit|stores/tsdb/index')
}

package() {
	mkdir -p "$pkgdir"/etc/$pkgname
	cp "$builddir"/cmd/loki/loki-local-config.yaml "$pkgdir"/etc/$pkgname
	install -Dm0755 cmd/$pkgname/$pkgname -t "$pkgdir"/usr/bin
	install -Dm0755 cmd/logcli/logcli -t "$pkgdir"/usr/bin

	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	install -Dm755 "$srcdir"/$pkgname-promtail.initd "$pkgdir"/etc/init.d/$pkgname-promtail
	install -Dm644 "$srcdir"/$pkgname-promtail.confd "$pkgdir"/etc/conf.d/$pkgname-promtail

	install -D clients/cmd/promtail/promtail-local-config.yaml -t "$pkgdir"/etc/$pkgname/
	install -Dm0755 clients/cmd/promtail/promtail -t "$pkgdir"/usr/bin/
}

logcli() {
	pkgdesc="Loki log client"

	amove usr/bin/logcli
}

promtail() {
	pkgdesc="Promtail client"

	amove etc/$pkgname/promtail-local-config.yaml
	amove usr/bin/promtail
}

promtail_openrc() {
	pkgdesc="Promtail client (Open RC init scripts)"
	install_if="openrc loki-promtail=$pkgver-r$pkgrel"

	amove etc/init.d/$pkgname-promtail
	amove etc/conf.d/$pkgname-promtail
}

sha512sums="
f599cfe8d722e47e3e5a1c6fa07c2b5f4d8b49a4f0aa2755875c7f5fec1f0787940446430dad26397d041e5571c9b3d2e61a1106a8eb9d56f2d4efe332c42d69  loki-2.9.4.tar.gz
469920d90daebf142db5dc5d3f2e972f59f5f5dc86684c4dcd5924a33ac7db305561006050ad42393296977ea27f1ce1cd78f78c71de351335167b41e4631a85  loki.initd
b4c281e191e7620ffe19d3fcc0b5669f54051a8108f0abc5b1aec2f2026452e02e3c26c3885e1e857cb6007bbc41c1f964e6d25d73bdfe170b67b63ecd9f14fd  loki.confd
f7d33f03d51c3cc5cda082e72f56ec821c60f70f42923a215652517835b66a68d7db3b246cd1a53ce0b2c003fe31f7e6cca06965ba792e92117d7e91abce829f  loki-promtail.confd
ff72431ba978880cc58914302b2ec1e48ef3d16b2a667dc82deaa3d2d890331170757dcac48ba0b145c988e70e85c240c94da335894d2c9d114fa811007f1111  loki-promtail.initd
302f14596207037fb7ee892e5c214e857c30cf358b00ae51be2e1639f7e0bbf005d63d16a6c9b3384174827d7ed5c70d873e08050c1635e8a8c5c930ca85df68  goflags.patch
59170bb5ab365179442c17683c64a94640549778ee7af76af9e5d0d6847aabe37e4bdf1f3a9db56c1e2b1218be0727f9980896f5cd8c477f6277ee768e7abbc2  enable-cgo.patch
"
